package com.mrd.gtimpframe.service.impl;

import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimpframe.entity.PlatformFee;
import com.mrd.gtimpframe.repository.PlatformFeeMapper;
import com.mrd.gtimpframe.service.PlatformFeeService;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import javax.annotation.Resource;

@Service
public class PlatformFeeServiceImpl implements PlatformFeeService {
    @Resource
    private PlatformFeeMapper platformFeeMapper;

    @Override
    @Transactional
    public int deleteByPrimaryKey(Object id) {
        return platformFeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int insert(PlatformFee record) {
        return platformFeeMapper.insert(record);
    }

    @Override
    @Transactional
    public int insertSelective(PlatformFee record) {
        return platformFeeMapper.insertSelective(record);
    }

    @Override
    public PlatformFee selectByPrimaryKey(Object id) {
        return this.platformFeeMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(PlatformFee record) {
        return this.platformFeeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByPrimaryKey(PlatformFee record) {
        return this.platformFeeMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据条件查询所有
     * @author han
     * @param platformFee PlatformFee对象
     * @return List<PlatformFee>
     */
    public List<PlatformFee> queryList(PlatformFee platformFee) {
        return this.platformFeeMapper.queryList(platformFee);
    }

    /**
     * @author dusy
     */
    @Override
    @Transactional
    public Result saveOrUpdate(PlatformFee platformFee) {
        String userId = LoginUtil.getLoginUserId();
        if (StringUtils.isEmpty(platformFee.getId())) {
            platformFee.setId(UUID.randomUUID().toString().replace("-", ""));
            platformFee.setCreator(userId);
            platformFee.setCtime(new Date());
            platformFee.setDelFlag("0");
            this.insertSelective(platformFee);
        } else {
            platformFee.setMidifier(userId);
            platformFee.setMtime(new Date());
            this.updateByPrimaryKeySelective(platformFee);
        }
        return Result.newSuccessInstance("操作成功", platformFee);
    }

    /**
     * @author dusy
     */
    @Override
    @Transactional
    public Result copyPlatformFee(String id) {
        PlatformFee platformFee = this.selectByPrimaryKey(id);
        if (platformFee != null) {
            platformFee.setId(UUID.randomUUID().toString().replace("-", ""));
            platformFee.setCreator(LoginUtil.getLoginUserId());
            platformFee.setCtime(new Date());
            platformFee.setDelFlag("0");
            platformFee.setStatus("0");//复制的默认为草稿
            platformFee.setMtime(null);
            platformFee.setMidifier(null);
            this.insert(platformFee);
        }else {
            return Result.newFailInstance("数据丢失");
        }
        return Result.newSuccessInstance("操作成功",platformFee);
    }

    /**
     * 批量删除
     *
     * @author han
     * @param id
     * @return
     */
    public int deleteBatch(Object[] id){
        return platformFeeMapper.deleteBatch(id);
    }

    /**
     * 更新状态
     * @author han
     * @param platformFee PlatformFee对象
     */
    public void confSatus(PlatformFee platformFee){
        platformFeeMapper.confSatus(platformFee);
    }

    /**
     * 批量逻辑删除
     * @author han
     * @param id
     * @return
     */
    public int delFlagBatch(Object[] id){
        return platformFeeMapper.delFlagBatch(id);
    }

    /**
     * 根据协议时间改变协议状态
     *
     * @author dusy
     */
    @Override
    public void updateStatusByTime() {
        platformFeeMapper.updateStatusByTime();
    }

    /**
     * 代录单计费查询可用的报价协议
     * @author han
     * @param map
     * @return
     */
    public List<PlatformFee> queryPlatformFeePay(Map<String,Object> map){
        return platformFeeMapper.queryPlatformFeePay(map);
    }
}
